package org.hy.service.mypage;

import java.util.List;

import org.hy.domain.User;
import org.hy.domain.group.GDiscuss;
import org.hy.domain.group.GMember;
import org.hy.domain.mypage.MyCareerShow;
import org.hy.domain.mypage.MyCareerShowComment;
import org.hy.domain.mypage.MyCareerShowContent;
import org.hy.domain.ug.PostCommonComment;
import org.hy.persistence.mypage.MyCareerShowMapper;
import org.hy.util.AppConstant;
import org.hy.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MyCareerShowServiceImpl implements IMyCareerShowService {
	@Autowired
	private MyCareerShowMapper myCareerShowMapper;
	
	@Override
	@Transactional
	public int addMyCareerShow(MyCareerShow myCareerShow, MyCareerShowContent myCareerShowContent){
		if(myCareerShow == null)return AppConstant.FAIL;
		return myCareerShowMapper.insertMyCareerShowContent(myCareerShowContent);
	}
	
	@Override
	@Transactional
	public  int addMyCareerShowComment(MyCareerShowComment myCareerShowComment){
		if(myCareerShowComment == null)return AppConstant.FAIL;
		return myCareerShowMapper.insertMyCareerShowComment(myCareerShowComment);
	}
	/**
	@Override
	@Transactional
	public int addMyCareerShowContent(MyCareerShowContent myCareerShowContent){
		if(myCareerShowContent == null)return AppConstant.FAIL;
		return myCareerShowMapper.insertMyCareerShowContent(myCareerShowContent);
	}
	*/
	@Override
	public MyCareerShow findMyCareerShowById(int id){
		return myCareerShowMapper.findMyCareerShowById(id);	
	}
	
	@Override
	public MyCareerShow findMyCareerShowWithCommentsById(int id,int page,int size){
		int offset = Pager.getOffset(page,size);
		return myCareerShowMapper.findMyCareerShowWithCommentsById(id, offset, size);
	}
	
	@Override
	public MyCareerShow findMyCareerShowWithDefaultCommentsById(int id,int page,int size){
		int offset = Pager.getOffset(page, size);
		return myCareerShowMapper.findMyCareerShowWithDefaultCommentsById(id, offset, size);
	}
	
	@Override
	public List<MyCareerShow> findMyCareerShowWithDefaultCommentsByUid(int uid,int page,int size){
		int offset = Pager.getOffset(page, size);
		return myCareerShowMapper.findMyCareerShowsWithDefaultCommentsByUid(uid, offset, size);
	}
	
	@Override
	@Transactional
	public int deleteMyCareerShow(int id,int ownerId){
		MyCareerShow mis = myCareerShowMapper.findMyCareerShowById(id);
		User actualOwner = (User)mis.getPoster();
		if(actualOwner.getId() != ownerId) throw AppConstant.ILLEGAL_REMOVE;
		return myCareerShowMapper.deleteMyCareerShow(id);
		//int id_mcs = id;
		//return myCareerShowMapper.deleteMyCareerShowContent(id_mcs);
	}

	@Override
	@Transactional
	public int deleteMyCareerShowComment(int idMcs, int ownerId,int page,int size,int idMcsc) {
		int offset = Pager.getOffset(page, size);
		MyCareerShow mcs = myCareerShowMapper.findMyCareerShowWithCommentsById(idMcs, offset, size);
		List<PostCommonComment> mcscList = mcs.getComments();
		PostCommonComment mcsc = mcscList.get(offset);
		User owner = (User)mcsc.getPoster();
		User mcsOwner = (User)mcs.getPoster();
		if(ownerId == owner.getId() || ownerId == mcsOwner.getId()){
			return myCareerShowMapper.deleteMyCareerShowComment(idMcsc);
		}
		return AppConstant.FAIL;
	}
	
	@Override
	@Transactional
	public int deleteMyCareerShowComment(int idMcsc,int ownerId){
		MyCareerShowComment mcsc = myCareerShowMapper.findMyCareerShowCommentById(idMcsc);
		User owner = (User)mcsc.getPoster();
		int mcsId = mcsc.getRefFeedId();
		MyCareerShow mcs = myCareerShowMapper.findMyCareerShowById(mcsId);
		User mcsOwner = (User)mcs.getPoster();
		if(ownerId == owner.getId() || ownerId == mcsOwner.getId()){
			return myCareerShowMapper.deleteMyCareerShowComment(idMcsc);
		}
		return AppConstant.FAIL;
	}

	@Override
	public MyCareerShow edit(int id, int ownerId) {
		MyCareerShow mcs = myCareerShowMapper.findMyCareerShowById(id);
		MyCareerShowContent mcsc = (MyCareerShowContent)mcs.getContent();//这个地方可能有BUG
		User owner = (User)mcs.getPoster();
		if(ownerId == owner.getId()){
			myCareerShowMapper.updateMyCareerShow(mcs);
			myCareerShowMapper.updateMyCareerShowContent(mcsc);
			MyCareerShow mcsEdited = myCareerShowMapper.findMyCareerShowById(id);
			return mcsEdited;
		}
		return mcs;
	}

	@Override
	public MyCareerShowComment editComment(int id, int ownerId) {
		MyCareerShowComment mcsc = myCareerShowMapper.findMyCareerShowCommentById(id);
		User owner = (User)mcsc.getPoster();
		if(ownerId == owner.getId()){
			myCareerShowMapper.updateMyCareerShowComment(mcsc);
			MyCareerShowComment mcscEdited = myCareerShowMapper.findMyCareerShowCommentById(id);
			return mcscEdited;
		}
		return mcsc;
	}
	
	

}
